package com.lbw.factory.abstractfactory.order;


import com.lbw.factory.abstractfactory.pizza.Pizza;
import com.sun.tools.javac.util.Assert;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author lbw
 * @version 1.0
 * @date 2021/10/15 20:47
 */
public class OrderPizza {

    private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    AbstractFactory abstractFactory;

    public OrderPizza(AbstractFactory abstractFactory) {
        this.setAbstractFactory(abstractFactory);
    }

    private void setAbstractFactory(AbstractFactory abstractFactory) {
        Pizza pizza = null;
        String orderType = "";
        this.abstractFactory = abstractFactory;
        do {
            orderType = getType();
            pizza = abstractFactory.createPizza(orderType);
            Assert.checkNonNull(pizza, "非常抱歉, 没有这种口味的披萨");
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();
        } while (true);
    }

    private String getType() {

        System.out.println("input pizza type: ");
        String type = null;
        try {
            type = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return type;
    }
}
